package com.ruoyi.product.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.product.domain.ProdImage;
import com.ruoyi.product.service.IProdImageService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 图片Controller
 *

 * @date 2023-07-20
 */
@RestController
@RequestMapping("/image")
public class ProdImageController extends BaseController {
    @Autowired
    private IProdImageService prodImageService;

    /**
     * 查询图片列表
     */
    @RequiresPermissions("product:image:list")
    @GetMapping("/list")
    public TableDataInfo list(ProdImage prodImage) {
        startPage();
        List<ProdImage> list;
        //判断如果商品id等于0
        if (prodImage.getProductId() != null && prodImage.getProductId() == 0) {
            // 查询轮播图数据
            list = prodImageService.selectSlideshowList();
        } else {
            // 查询普通商品图片数据
            list = prodImageService.selectProdImageList(prodImage);
        }
        return getDataTable(list);
    }


    /**
     * 导出图片列表
     */
    @RequiresPermissions("product:image:export")
    @Log(title = "图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProdImage prodImage) {
        List<ProdImage> list = prodImageService.selectProdImageList(prodImage);
        ExcelUtil<ProdImage> util = new ExcelUtil<ProdImage>(ProdImage.class);
        util.exportExcel(response, list, "图片数据");
    }

    /**
     * 获取图片详细信息
     */
    @RequiresPermissions("product:image:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(prodImageService.selectProdImageById(id));
    }

    /**
     * 获取图片详细信息
     */
    @RequiresPermissions("product:image:query")
    @GetMapping(value = "/image/{id}")
    public AjaxResult getImages(@PathVariable("id") Long id) {
        return success(prodImageService.selectProdImageByProId(id));
    }

    /**
     * 新增图片
     */
    @RequiresPermissions("product:image:add")
    @Log(title = "图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProdImage prodImage) {
        return toAjax(prodImageService.insertProdImage(prodImage));
    }

    /**
     * 修改图片
     */
    @RequiresPermissions("product:image:edit")
    @Log(title = "图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProdImage prodImage) {
        return toAjax(prodImageService.updateProdImage(prodImage));
    }

    /**
     * 删除图片
     */
    @RequiresPermissions("product:image:remove")
    @Log(title = "图片", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(prodImageService.deleteProdImageByIds(ids));
    }
}
